/**
 * @file weight.c
 * @brief 
 * @author xuesong (wenshaoliu@foxmail.com)
 * @version 1.0
 * @date 2024-11-06
 * 
 * @copyright Copyright (c) 2024  by xuesong
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-11-06     <td>1.0     <td>xuesong   <td>内容
 * </table>
 */
#include "stdlib.h"
#include "weight.h"
//---------------------------------------------
static AutoWeightWeakUp_t AutoWeightWeakUp;
//---------------------------------------------
void AutoWeightUpInitStrcut(void)
{
    memset((uint8_t *)&AutoWeightWeakUp, 0, sizeof(AutoWeightWeakUp));
    AutoWeightWeakUp.WeakUpAdcValue = 0x113F;
    // if(userAdcStruct.Flag.Bit.locked)
    {
        AutoWeightWeakUp.ZeroAdcValue = 0x81A541;
    }
    // else{
    //     AutoWeightWeakUp.ZeroAdcValue = 0xFFFFFF;
    //     AutoWeightWeakUp.Flag.Bits.FastScan = true;
    // }
    printf("auto weakUp up_adc=0x%X,Zero=0x%X\n", AutoWeightWeakUp.WeakUpAdcValue, AutoWeightWeakUp.ZeroAdcValue);
}
//---------------------------------------------
/**
 * @brief
 * @param  adcValue         read Adc value
 * @return true             Weak Up
 * @return false            No need WeakUp
 */
bool AutoWeightUpProcess(uint32_t adcValue)
{
    bool isWeakUp = false;
    int32_t SubAdcValue = adcValue - AutoWeightWeakUp.lastAdcValue;
    SubAdcValue = abs(SubAdcValue);
    if (SubAdcValue > (AutoWeightWeakUp.WeakUpAdcValue >> 1)) // 不稳定
    {
        AutoWeightWeakUp.Flag.Bits.Load = false;
        AutoWeightWeakUp.StableCnt = 0;
        AutoWeightWeakUp.SumAdcValue = 0;
        AutoWeightWeakUp.SumCount = 0;
    }
    else
    {
        AutoWeightWeakUp.StableCnt++;
        AutoWeightWeakUp.SumAdcValue += adcValue;
    }
    AutoWeightWeakUp.lastAdcValue = adcValue;

    SubAdcValue = adcValue - AutoWeightWeakUp.ZeroAdcValue;
    AutoWeightWeakUp.Flag.Bits.Negative = false;
    if (SubAdcValue < 0)
    {
        AutoWeightWeakUp.Flag.Bits.Negative = true;
        SubAdcValue = 0 - SubAdcValue;
    }
    if (SubAdcValue > AutoWeightWeakUp.WeakUpAdcValue)
    {
        if (!AutoWeightWeakUp.Flag.Bits.Load) // 首次加载
        {
            AutoWeightWeakUp.StableCnt = 0;
            AutoWeightWeakUp.SumAdcValue = 0;
            AutoWeightWeakUp.SumCount = 0;
        }
        AutoWeightWeakUp.Flag.Bits.Load = true;
        AutoWeightWeakUp.Flag.Bits.FastScan = true; // 开启快速 扫描

        AutoWeightWeakUp.LoadCount++;
        if (AutoWeightWeakUp.Flag.Bits.Negative)
        {
            AutoWeightWeakUp.LoadCount = 0;
        }
    }
    else
    {
        AutoWeightWeakUp.LoadCount = 0;
        // 差值小于开机力且大于分度值，置起分度标志
        if (SubAdcValue > (AutoWeightWeakUp.WeakUpAdcValue >> 1) && !AutoWeightWeakUp.Flag.Bits.Negative)
        {
            AutoWeightWeakUp.Flag.Bits.ScaleDivision = true;
        }
        else
        {
            // 差值小于开机力且小于分度值，则退出快速判断和加载标志
            AutoWeightWeakUp.Flag.Bits.ScaleDivision = false;
            AutoWeightWeakUp.Flag.Bits.Load = false;
            AutoWeightWeakUp.UnLoadCount++;
            if (AutoWeightWeakUp.UnLoadCount > 15) // 放宽一次判稳范围
            {
                AutoWeightWeakUp.Flag.Bits.Relax = true;
            }
        }
    }
    if (AutoWeightWeakUp.StableCnt >= 4) // 稳定次数达到
    {
        // 负值或者小于压重开机唤醒值则刷零
        if (AutoWeightWeakUp.Flag.Bits.Negative || (!AutoWeightWeakUp.Flag.Bits.ScaleDivision && !AutoWeightWeakUp.Flag.Bits.Load))
        {
            AutoWeightWeakUp.ZeroAdcValue = AutoWeightWeakUp.SumAdcValue / AutoWeightWeakUp.StableCnt;
        }
        else if (AutoWeightWeakUp.Flag.Bits.Load)
        {
            isWeakUp = true;
        }
        AutoWeightWeakUp.Flag.Bits.zeroOk = true;
        AutoWeightWeakUp.Flag.Bits.Load = false;
        AutoWeightWeakUp.Flag.Bits.Relax = false;
        AutoWeightWeakUp.Flag.Bits.FastScan = false; // 停止快速 扫描

        AutoWeightWeakUp.StableCnt = 0;
        AutoWeightWeakUp.SumAdcValue = 0;
        AutoWeightWeakUp.SumCount = 0;
    }
    else
    {
        if (AutoWeightWeakUp.LoadCount > 7) // 连续多次大于开机，立即开机
        {
            isWeakUp = true;
        }
    }
    return isWeakUp;
}

